// pages/list/list.js
var util = require('../../utils/util.js')
//var xiaojueding = require('../../utils/xiaojueding.js');
var xiaojueding 
var app = getApp()
Page({
   data: {
      xiaojueding: '',
      myxiaojueding: [],
      tab_index: 1,
      userId:'',
      id:''//类型：真心话、大冒险
   },

   
 //点赞
upvote(e) {
   var that = this
   wx.request({
     url: app.mainurl + 'upvote/add', //仅为示例，并非真实的接口地址
     data: {
      hearttackId:e.currentTarget.dataset.id,
      userId:that.data.userId
     },
     method: 'POST',
     header: {
       'content-type': 'application/x-www-form-urlencoded'
     },
     success(res) {
         wx.showToast({
            title: '点赞成功',
            icon: 'success',
            mask: false
         })
         that.getlist(that.data.id)
     }
   })
 },
  

 //取消点赞
 cancelupvote(e) {
   var that = this

   wx.request({
     url: app.mainurl + 'upvote/remove', //仅为示例，并非真实的接口地址
     data: {
      ids:e.currentTarget.dataset.id
     },
     method: 'POST',
     header: {
       'content-type': 'application/x-www-form-urlencoded'
     },
     success(res) {
         wx.showToast({
            title: '取消点赞成功',
            icon: 'success',
            mask: false
         })
         that.getlist(that.data.id)
     }
   })
 },

   //复制
   copy(e) {
     var that=this
      wx.request({
         url: app.mainurl + 'hearttack/copy', //仅为示例，并非真实的接口地址
         data:{
            userId:that.data.userId,
            hearttackId:e.currentTarget.dataset.id
         },
         method: 'POST',
         header: {
            'content-type': 'application/x-www-form-urlencoded'
         },
         success(res) {
            wx.showToast({
               title: '复制成功！',
               icon: 'success',
               mask: false
            })
            that.getlist(that.data.id)
            that.getlistPerson(that.data.id)
         }
       })
   
},
  //删除个人的真心话或者大冒险
  delete(e){
   var that = this
   wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
         wx.request({
            url: app.mainurl + 'hearttack/remove', //仅为示例，并非真实的接口地址
            data: {
             ids:e.currentTarget.dataset.id,
            },
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success(res) {
              
             wx.showToast({
                title: '删除成功！',
                icon: 'success',
                mask: false
             })
             //刷新个人小决定
             that.getlistPerson(that.data.id)
             that.getlist(that.data.id)
            }
          })

        } else if (sm.cancel) {
         console.log('用户点击取消')
        }
     }
   })
   
},
  

   onLoad: function (options) {
      this.setData({
         userId:options.userId,
         id:options.id
      })
     
      
   },
//查询真心话或者大冒险列表所有
getlist(id) {
   var that = this
   wx.request({
     url: app.mainurl + 'hearttack/getList', //仅为示例，并非真实的接口地址
     data: {
       type:id,
       currentUserId:that.data.userId
     },
     method: 'POST',
     header: {
       'content-type': 'application/x-www-form-urlencoded'
     },
     success(res) {
       var datas = res.data.data
       console.log(datas)
       that.setData({
         xiaojueding: datas
      })
      xiaojueding=datas
     
     }
   })
 },
//查询真心话或者大冒险列表（个人）
getlistPerson(id) {
   var that = this
   wx.request({
     url: app.mainurl + 'hearttack/getList', //仅为示例，并非真实的接口地址
     data: {
       type:id,
       userId:that.data.userId
     },
     method: 'POST',
     header: {
       'content-type': 'application/x-www-form-urlencoded'
     },
     success(res) {
       var datas = res.data.data
       
       that.setData({
         myxiaojueding: datas
      })
   
     }
   })
 },

   //热门、个人小决定
   tabSwitch(e) {
      var that = this, flg = e.currentTarget.dataset.flg, myJuedin = wx.getStorageSync('myJuedin');

      if (flg == 2) {
         that.setData({
            myxiaojueding: that.data.myxiaojueding
         })
      }
      that.setData({
         tab_index: flg == 1 ? '1' : '2'
      })
   },

   //添加个人小决定
   addPersonalQ(e) {
      var that=this
      wx.navigateTo({
         url: '../edit/edit?type='+that.data.id+"&userId="+that.data.userId+"&hearttackId=0",
      })
   },

   //个人编辑
   personalQToRevise(e) {
      var that=this
    
      wx.navigateTo({
         url: '../edit/edit?type='+that.data.id+"&userId="+that.data.userId+"&hearttackId="+e.currentTarget.dataset.id+"&name="+e.currentTarget.dataset.name,
      })
   },

   //热门编辑
   officialQToRevise(e) {
      var that = this, xiaojueding = that.data.xiaojueding, index = e.currentTarget.dataset.index;
      for (let i in xiaojueding) {
         if (i == index) {
            wx.navigateTo({
               url: '../edit/edit?flg=1&item=' + JSON.stringify(xiaojueding[i])
            })
            return;
         }
      }
   },

   //个人决定右边的图片
   personalQToControl(e) {
      var that = this, index = e.currentTarget.dataset.index, idx, myxiaojueding = that.data.myxiaojueding;

      for (let x in myxiaojueding) {
         if (x == index) {
            idx = myxiaojueding[x].num1 == undefined ? index : undefined;
            myxiaojueding[x].num1 = idx;
         } else {
            myxiaojueding[x].num1 = undefined;
         }
      }
      that.setData({
         myxiaojueding: myxiaojueding
      })
   },

   //热门决定右边的图片
   officialQToControl(e) {
      var that = this, index = e.currentTarget.dataset.index, idx;
      
      for (let x in xiaojueding) {
         if (x == index) {
            idx = xiaojueding[x].num == undefined ? index : undefined;
            xiaojueding[x].num = idx;
         } else {
            xiaojueding[x].num = undefined;
         }
      }
      that.setData({
         xiaojueding: xiaojueding
      })
   },


   //转到真心话或者大冒险
  ToRun(e) {
      var that = this
      wx.navigateTo({
         url: '../truth-adventure/index?problemType='+that.data.id+'&id='+e.currentTarget.dataset.id,
       })
   },

   //个人决定的标题
   personalQToRun(e) {
      var that = this, id = e.currentTarget.dataset.id;
      app.globalData.myJueding = true;
      wx.setStorageSync('switchTab', id);
      wx.switchTab({
         url: '../index/index'
      })
   },

   onShow: function () {
     
      var that = this;

      this.getlist(that.data.id)
      //个人小决定
      this.getlistPerson(that.data.id)
      app.globalData.defaultJueding = false, app.globalData.myJueding = false;

      

      wx.removeStorageSync('switchTab')
      
      
   },

   onShareAppMessage: function () {
      let that = this;
      mta.Event.stat("share", { 'time': '1' });
      var picNum = Math.floor(Math.random() * 4 + 1);//获取1-4的随机数，用于随机展示分享图片

      return {
         title: util.isNull(app.globalData.shareTitle) ? ("一起来玩'" + app.globalData.title + "'吧") : app.globalData.shareTitle,
         path: '/pages/index/index',
         success: function (res) {
            console.log('成功进入分享==========', res);

         },
         fail: function (res) {
            console.log('进入分享失败==========', res);
         }
      }
   }
})